//set working directory 
cd  "C:\Users\johna\Dropbox (Curiel Analytx)\ElectionNightResults\replication_code"

//read in the state dta file 
use "data\nyt_states.dta", clear
sort state time
drop total // Don't need this.  We'll generate two-party total later


*** Calculate when Biden or Trump were certain to win

** Note:  for now, we had to fudge the total number of votes, because there are
** times when the reported vote totals exceed the number of votes Leip reports

egen max_leip_tvotes = max(leip_tvotes),by(state)
egen max_total = max(total), by(state)

egen rowmax_tvotes_total = rowmax(max_leip_tvotes max_total)


gen final_D = .5*leip_tvotes
gen D_needpct = 100*(final_D - dem)/(rowmax_tvotes_total - dem - rep)
gen left = 100*(final_D - dem)
gen right = (rowmax_tvotes - dem - rep)

replace norm_total = 100*(dem + rep)/rowmax_tvotes_total


** Find the place where the winner is inevitable

gen winner =     1 if leip_dempct > 50 & D_needpct < 0 & D_needpct[_n-1] > 0 & state == state[_n-1] // Biden states
replace winner = 1 if leip_dempct < 50 & D_needpct > 100 & D_needpct[_n-1] < 100 & state == state[_n-1] // Trump states
keep if winner == 1
sort state time_num
bysort state:gen n=_n
keep if n == 1

merge m:1 state using "data/scratch/call_times",nogen

gen evotes = evotes1 + evotes2
drop evotes1 evotes2

keep time time_num state evotes* dempct call_time
gen D_evotes = evotes if dempct > 50
gen R_evotes = evotes if dempct < 50


** order of call given mathematical impossibility

sort time_num

gen cumR = R_evotes in 1
gen cumD = D_evotes in 1
recode cum* *evotes* (.=0)
replace cumR = cumR[_n-1] + R_evotes in 2/l
replace cumD = cumD[_n-1] + D_evotes in 2/l

gen d_time_num  = dofc(time_num)
gen hh_time_num = hh(time_num)
gen mm_time_num = mm(time_num)

gen short_time_num = d_time_num + (hh_time_num/24) + (mm_time_num/(24*60))


** order of AP call

sort call_time

gen APcumR = R_evotes in 1
gen APcumD = D_evotes in 1
recode APcum* (.=0)
replace APcumR = APcumR[_n-1] + R_evotes in 2/l
replace APcumD = APcumD[_n-1] + D_evotes in 2/l

gen d_call_time  = dofc(call_time)
gen hh_call_time = hh(call_time)
gen mm_call_time = mm(call_time)

gen short_call_time = d_call_time + (hh_call_time/24) + (mm_call_time/(24*60))


** order of certification

merge 1:1 state using "data/certification_date"

sort certification_num

gen CERTcumR = R_evotes in 1
gen CERTcumD = D_evotes in 1
recode CERTcum* (.=0)
replace CERTcumR = CERTcumR[_n-1] + R_evotes in 2/l
replace CERTcumD = CERTcumD[_n-1] + D_evotes in 2/l

gen d_cert_num  = dofc(certification_num)
gen hh_cert_num = hh(certification_num)
gen mm_cert_num = mm(certification_num)

gen short_cert_num = d_cert_num + (hh_cert_num/24) + (mm_cert_num/(24*60))



**gen month = mofd(dofc(time_num))

sort short_cert_num CERTcumD
twoway (line APcumD short_call_time, sort lcolor(blue) lpattern(solid) connect(stairstep)) ///
       (line cumD short_time_num, sort lcolor(blue) lpattern(dash) connect(stairstep)) ///
	   (line CERTcumD short_cert_num, sort lcolor(blue) lpattern(shortdash) connect(stairstep)) ///
	   , ///
	   ytitle(Electoral Votes) ///
	   yline(270) ///
	   ylabel(0(50)300) ///
	   ymtick(none, nogrid) ///
	   xlabel(22220 "11/1" 22227 "11/8" 22234 "11/15" 22241 "11/22" 22248 "11/29" 22255 "12/6" 22262 "12/13") ///
	   xmtick(22220(1)22262, grid) ///
	   legend(order(1 "AP call" 2 "Mathematical futility" 3 "Certification")) ///
	   text(284 22226.48 "Nov. 7",size(*.5) placement(11)) ///
	   text(280 22232.18 "Nov. 13",size(*.5) placement(10)) ///
	   text(282 22256.5 "Dec. 7",size(*.5) placement(11))
graph export "figures\fig9.png", replace width(2500)
	   
	   
